import 'package:flutter/material.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';

/// 封装虚线分隔线，可控制颜色、点长、间隙
class DottedLine extends StatelessWidget {
  final Color color; // 虚线颜色
  final double dashLength; // 虚线点的长度
  final double dashGap; // 点之间的间隙

  const DottedLine({
    Key? key,
    this.color = AppColors.mallLineColorDF,
    this.dashLength = 3,
    this.dashGap = 3,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: Size.fromHeight(10), // 控制虚线的高度
      painter: DottedLinePainter(
        color: color,
        dashLength: dashLength,
        dashGap: dashGap,
      ),
    );
  }
}

/// 自定义绘制器，负责绘制虚线
class DottedLinePainter extends CustomPainter {
  final Color color;
  final double dashLength;
  final double dashGap;

  DottedLinePainter({
    required this.color,
    required this.dashLength,
    required this.dashGap,
  });

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint =
        Paint()
          ..color = color
          ..style = PaintingStyle.stroke
          ..strokeWidth = 1.0; // 虚线宽度

    Path path = Path();
    double startX = 0;
    // 循环绘制点和间隙，直到超出画布宽度
    while (startX < size.width) {
      path.moveTo(startX, size.height / 2); // 起点（中心位置）
      path.lineTo(startX + dashLength, size.height / 2); // 终点（点结束）
      startX += dashLength + dashGap; // 移动到下一个起点
    }

    canvas.drawPath(path, paint); // 绘制路径
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return oldDelegate != this; // 当参数变化时重新绘制
  }
}
